import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/Home.vue'

function importRoutes(r:any) {
  let routes = r.keys().map((key:any) => r(key).default);
  return flatten(routes);
}
const flatten:any = (arr:any[]) =>
  arr.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);

const children:any = importRoutes(require.context('../', true, /^\.\/layout\/((?!\/)[\s\S])+\/route\.ts$/));

const routes: Array<RouteRecordRaw> = [
  {
    path:'/403',
    name:'403',
    component: () => import('../layout/error/403.vue')
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      keepAlive: false
    }
  },
  ...children
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router
